package com.example.controller;

import com.example.common.utils.R;
import com.example.entity.User;
import com.example.service.ImFriendService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 验证码生成接口
 */
@CrossOrigin
@RestController
@RequestMapping("/friend")
@Api(tags = "好友功能")
public class ImFriendController {

    @Autowired
    private ImFriendService imFriendService;
    @RequestMapping("/SearchFriend")
    @ApiOperation(value = "搜索好友", notes = "根据用户名搜索用户")
    public R SearchFriend(@RequestParam String username) throws Exception {
        User user = imFriendService.SearchFriend(username);
        return R.success(user);
    }


    @RequestMapping("/SendApply")
    @ApiOperation(value = "申请好友请求", notes = "申请好友请求")
    public R SendApply(@RequestParam Integer userid) {
        try {
            imFriendService.SendApply(userid);
            return R.success();
        } catch (RuntimeException e) {
            // 如果已经发送过申请
            return R.error(e.getMessage());
        }
    }

    @RequestMapping("/ApplyList")
    @ApiOperation(value = "展示请求列表", notes = "查询请求列表")
    public R getApplyList() throws Exception {
        List<User> UserList = imFriendService.getApplyList();
        return R.success(UserList);
    }
    @RequestMapping("/ShowFriendsList")
    @ApiOperation(value = "查询all好友列表", notes = "查询all好友列表")
    public R getFriendsList() throws Exception {
        List<User> UserList = imFriendService.getFriendsList();
        return R.success(UserList);
    }

    @RequestMapping("/AgreeApply")
    @ApiOperation(value = "同意好友请求", notes = "同意好友请求")
    public R AgreeApply(@RequestParam Integer userId) throws Exception {
        imFriendService.AgreeApply(userId);
        return R.success();
    }
}
